package web.mb;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import metier.entities.Abonnement;
import metier.entities.Client;
import metier.entities.ClientAbonnement;
import metier.entities.ClientAbonnementId;
import metier.entities.Etat;
import metier.entities.GLN;
import metier.entities.Industrie;
import metier.entities.Langue;
import metier.entities.Pays;
import metier.entities.Transport;
import metier.entities.TransportCat;
import metier.entities.Ville;
import metier.sess.IEtatLocal;
import metier.sess.IIndustrieLocal;
import metier.sess.ILangueLocal;
import metier.sess.IPaysLocal;
import metier.sess.IStrategiePrixLocal;
import metier.sess.ITransportCatLocal;
import metier.sess.ITransportLocal;
import metier.sess.IUtilisateurLocal;
import metier.sess.IVilleLocal;


@ManagedBean
@ViewScoped
public class InscriptionMB {
	
	@EJB 
	private IUtilisateurLocal metier;
	
	@EJB 
	private IPaysLocal meteirp;
	
	@EJB
	private IEtatLocal metiere;
	
	@EJB 
	private IVilleLocal meteirv;
	
	//metier industrie
	@EJB
	private IIndustrieLocal metierIndustry;
	
	
	//Transport metier
	@EJB
	private ITransportLocal metiertr;
	
	
	
	//Trand
	@EJB
	private ITransportCatLocal metiertrcat;
	
	
	@EJB 
	private IStrategiePrixLocal metierPrix;
	
	@EJB
	private ILangueLocal metierLangue;
	
	
	//id de la langue selectionée
	
	//langue de l'uilisateur
	private Integer IdLangue;
	
      //cid
	private Long cid;
	
	//repeat password
	private String PasswordRe;
	
	//l'identifiant de l'abonnement 
	static private Integer IdAbonnement;
	//Intitule de l'abonnement
	static private  Abonnement abn;
	
	//date debut de l'abonnement
	private Date DateDebAbn;
	
	static private Client client=new Client();
	
	static private Integer Id;
	
	//type de transport
	static private Integer Idtransport;
	
	//country,state,city for contact person
	static private Integer IdS;
	
	static private Integer IdC;
	
	static private Integer IdCi;
	
	//country,state,city for Company 
		static private Integer IdScm;
		
		static private Integer IdCcm;
		
		static private Integer IdCicm;
		
	//country,state,city for Postal Address
		static private Integer IdSp;
		
		static private Integer IdCp;
		
		static private Integer IdCip;
		
	
	
	static private Integer IdIndustry;
	
   
	//list  country,state,city for contact person
	static private Set<Etat> listeEtat;
	static private Set<Ville> listeVille;
	
	//list  country,state,city for  Company 
	static private Set<Etat> listeEtatcm;
	static private Set<Ville> listeVillecm;
	
	
	//list  country,state,city for Postal Address 
	static private Set<Etat> listeEtatp;
	static private Set<Ville> listeVillep;
	
	//User Type temporaire ou permanant
	static private String userType;
	
	//Transport type
	static private List<String> listeTransport=new ArrayList<String>();
	
	
	//list des sous transports

	static private List<TransportCat> listTransportCat;
	static private List<String> listIdentifiantTransCat=new ArrayList<String>();
	
	
	private List<String>  listeTest=new ArrayList<String>();
	
	//les termes
	private boolean terme1;
	private boolean terme2;
	private boolean terme3;
	
	
	
	
	public boolean isTerme1() {
		return terme1;
	}


	public void setTerme1(boolean terme1) {
		this.terme1 = terme1;
	}


	public boolean isTerme2() {
		return terme2;
	}


	public void setTerme2(boolean terme2) {
		this.terme2 = terme2;
	}


	public boolean isTerme3() {
		return terme3;
	}


	public void setTerme3(boolean terme3) {
		this.terme3 = terme3;
	}


	public Integer getIdLangue() {
		return IdLangue;
	}


	public void setIdLangue(Integer idLangue) {
		IdLangue = idLangue;
	}


	public String getPasswordRe() {
		return PasswordRe;
	}


	public void setPasswordRe(String passwordRe) {
		PasswordRe = passwordRe;
	}


	public  String getUserType() {
		return userType;
	}


	public  void setUserType(String userType) {
		InscriptionMB.userType = userType;
	}


	public  List<String> getListIdentifiantTransCat() {
		return listIdentifiantTransCat;
	}


	public  void setListIdentifiantTransCat(
			List<String> listIdentifiantTransCat) {
		InscriptionMB.listIdentifiantTransCat = listIdentifiantTransCat;
	}


	//listegln
	static private List<String> listgln=new ArrayList<String>();
	
	//date d'expiration d'un gln
	static private List<Date> listeExpgln=new ArrayList<Date>();
	

	//méthode init
	
	public Long getCid() {
		System.out.println("get"+cid);
		return cid;
	}


	public void setCid(Long cid) {
		this.cid = cid;
	}


	@PostConstruct
	public void init()
	{
		listgln.add("");
		listeExpgln.add(null);
		
	}
	
	
	public  List<Date> getListeExpgln() {
		return listeExpgln;
	}



	public  void setListeExpgln(List<Date> listeExpgln) {
		this.listeExpgln = listeExpgln;
	}



	

	


	public  Abonnement getAbn() {
		return abn;
	}


	public  void setAbn(Abonnement abn) {
		this.abn = abn;
	}


	public  List<String> getListgln() {
		return listgln;
	}


	public  void setListgln(List<String> listgln) {
		this.listgln = listgln;
	}


	public List<String> getListeTest() {
		return listeTest;
	}

	public void setListeTest(List<String> listeTest) {
		this.listeTest = listeTest;
	}


	public Integer getId() {
		return Id;
	}


	public void setId(Integer id) {
		Id = id;
	}


	public Integer getIdS() {
		return IdS;
	}


	public void setIdS(Integer idS) {
		IdS = idS;
	}


	public Integer getIdC() {
		return IdC;
	}


	public void setIdC(Integer idC) {
		IdC = idC;
	}


	public Integer getIdCi() {
		return IdCi;
	}


	public void setIdCi(Integer idCi) {
		IdCi = idCi;
	}


	public Integer getIdIndustry() {
		return IdIndustry;
	}


	public void setIdIndustry(Integer idIndustry) {
		IdIndustry = idIndustry;
	}




	public  List<String> getListeTransport() {
		System.out.println("liste"+listeTransport.size());
		return listeTransport;
	}


	public  void setListeTransport(List<String> listeTransport) {
		this.listeTransport = listeTransport;
	}


	public  Client getClient() {
		return client;
	}


	public  void setClient(Client client) {
		this.client = client;
	}





	public  Set<Etat> getListeEtat() {

		return listeEtat;
	}


	public  void setListeEtat(Set<Etat> listeEtat) {
		this.listeEtat = listeEtat;
	}


	
	
    
	 public  Set<Ville> getListeVille() {

		return listeVille;
	}


	public  void setListeVille(Set<Ville> listeVille) {
	this.listeVille = listeVille;
	}


	public List<Industrie> listeIndustrie()
	 {
		 return metierIndustry.getAllActiveIndustrie();
	 }
	 
	 

	public  Integer getIdScm() {
		return IdScm;
	}


	public  void setIdScm(Integer idScm) {
		IdScm = idScm;
	}


	public  Integer getIdCcm() {
		return IdCcm;
	}


	public  void setIdCcm(Integer idCcm) {
		IdCcm = idCcm;
	}


	public  Integer getIdCicm() {
		return IdCicm;
	}


	public  void setIdCicm(Integer idCicm) {
		IdCicm = idCicm;
	}


	public  Integer getIdSp() {
		return IdSp;
	}


	public  void setIdSp(Integer idSp) {
		IdSp = idSp;
	}


	public  Integer getIdCp() {
		return IdCp;
	}


	public  void setIdCp(Integer idCp) {
		IdCp = idCp;
	}


	public  Integer getIdCip() {
		return IdCip;
	}


	public  void setIdCip(Integer idCip) {
		IdCip = idCip;
	}

     
	
	public Integer getIdAbonnement() {
		return IdAbonnement;
	}


	public void setIdAbonnement(Integer idAbonnement) {
		IdAbonnement = idAbonnement;
	}


	public  Set<Etat> getListeEtatcm() {
		return listeEtatcm;
	}
    
	



	public  void setListeEtatcm(Set<Etat> listeEtatcm) {
		this.listeEtatcm = listeEtatcm;
	}


	public  Set<Ville> getListeVillecm() {
		return listeVillecm;
	}


	public  void setListeVillecm(Set<Ville> listeVillecm) {
		this.listeVillecm = listeVillecm;
	}


	public  Set<Etat> getListeEtatp() {
		return listeEtatp;
	}


	public  void setListeEtatp(Set<Etat> listeEtatp) {
		this.listeEtatp = listeEtatp;
	}


	public  Set<Ville> getListeVillep() {
		return listeVillep;
	}


	public  void setListeVillep(Set<Ville> listeVillep) {
		this.listeVillep = listeVillep;
	}

	
    public  Integer getIdtransport() {
		return Idtransport;
	}


	public  void setIdtransport(Integer idtransport) {
		Idtransport = idtransport;
	}


	public  List<TransportCat> getListTransportCat() {
		return listTransportCat;
	}


	public  void setListTransportCat(List<TransportCat> listTransportCat) {
		this.listTransportCat = listTransportCat;
	}


	//list des Transports 
	public List<Transport> listTransport()
	{
		return metiertr.getAllTransport();
	}
	
	public Date getDateDebAbn() 
	{
		return DateDebAbn;
	}

	public void setDateDebAbn(Date dateDebAbn) {
		DateDebAbn = dateDebAbn;
	}

	//Type="Tra" ou "Exp"  Category="Pri" ou "Pro"
	public List<Client> listeClients(String Type,String Category)
	{
	  return metier.listClient(Type, Category);
	}
	
	//liste des abonnements 
	public List<Abonnement> listeAbonnement()
	{
		return metierPrix.listeAbonnementActive(0);
	}
	 
  
	public String View(Integer id)
	{
		client=(Client) metier.getUtilisateur(id);
		List<GLN> listegln=client.getList_GLN();
		
		
		for(int i=0;i<listegln.size();i++)
		{
			
			listgln.set(i, listegln.get(i).getNumero_GLN());
			listeExpgln.set(i, listegln.get(i).getDate_Expiration());
			
		}
		
		
		int i;
		if(client.getCABs().size()!=0)
		{
			for(i=0;i<client.getCABs().size();i++)
			{
				if(client.getCABs().get(i).getDateFin().compareTo(new Date())>0)
				{
					
					userType="Permanent";
				    abn=client.getCABs().get(i).getPk().getAbonnement();
					break;
				}
				
			}
			
				if(i>=client.getCABs().size())
				{
					userType="Temporary";
				}
					
		}
		else
			userType="Temporary";
		System.out.println(userType);
		
		
		if(client.getTranExp().compareTo("Exp")==0)
		{
			if(client.getType_client().compareTo("Pri")==0)
			return "ViewPrivateShippers.xhtml";
			else
			return "ViewProShippers.xhtml";	
		}
		else
		{
			if(client.getType_client().compareTo("Pri")==0)
			return "ViewPrivateCarrier.xhtml";
			else
			return "ViewProCarriers.xhtml";	
		}
	}
	
	
	public String AjouterFrontEnd(String type,String cat)
	{
		//vérifier la validité des champs
		//vérifier le champs password
		boolean a=true;
		if(!verify())
		{
	     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "error password doesn't match "));
		a=false;
		}
		
		//vérifier si les termes sont cochés
		if(!(terme1 && terme2 && terme3))
		{
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "all terms must be checked"));
				a=false;
		}
		
		
		if(a)
		{
		//si tous les champs sonts valides
		client.setVille_Utilisateur(meteirv.getVille(IdCi));
		client.setIndustrie(metierIndustry.getIndustrie(IdIndustry)); 
		client.setTranExp(type);
		client.setType_client(cat);
		
		if(type.compareTo("Tra")==0)
		{
		List<TransportCat> liste=new ArrayList<TransportCat>();
		
		for(int i=0;i<listIdentifiantTransCat.size();i++)
		{
			liste.add(metiertrcat.getTransportCat(Integer.parseInt(listIdentifiantTransCat.get(i))));
		}
		
		client.setListTransportTrCat(liste);
		}
		if(cat.compareTo("Pro")==0)
		{
			
		//s'il s'agit d'un client professionnelle
		//transport categories
		
		
		//ville de la société
		client.setVille_Societe(meteirv.getVille(IdCicm));
		
		//gln
		GLN gln=new GLN(meteirv.getVille(IdCicm), client,listeExpgln.get(0), listgln.get(0), client.getRue_Societe(), true);
		
		client.getList_GLN().add(gln);
		//ville postal
		client.setVille_postal(meteirv.getVille(IdCip));
		
		//abonnement
		//récupérer l'abonnement concerné

		if(this.userType.compareTo("Temporary")!=0)
		{
		
			Abonnement abonnement=(Abonnement) metierPrix.getStrategiePrix(IdAbonnement);
			
			ClientAbonnementId clabi=new ClientAbonnementId(client,(Abonnement) metierPrix.getStrategiePrix(IdAbonnement));
			ClientAbonnement cbn=new ClientAbonnement();
			cbn.setPk(clabi);
			//date Début
			SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
			
			Calendar DateDebAbn = Calendar.getInstance();
			
			cbn.setDateDebut(DateDebAbn.getTime());
			
			if(abonnement.getPeriode().compareTo("Month")==0)
			DateDebAbn.add(Calendar.MONTH,abonnement.getNbrPeriode());
			else
			DateDebAbn.add(Calendar.YEAR,abonnement.getNbrPeriode());
			//date fin
			cbn.setDateFin(DateDebAbn.getTime());
			//ajouter l'abonnement
			
		      client.getCABs().add(cbn);
		}
		
		}
		
		//cid
		
		client.setCID(cid);
		client.setLangue(metierLangue.getLangue(IdLangue));
		metier.addUtilisateur(client);
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"", "succes"));
		
			
		}
		
		return "ici";
		
	}

	
	public String Ajouter(String type,String cat)
	{
		
		client.setVille_Utilisateur(meteirv.getVille(IdCi));
		client.setIndustrie(metierIndustry.getIndustrie(IdIndustry)); 
		client.setTranExp(type);
		client.setType_client(cat);
		
		if(type.compareTo("Tra")==0)
		{
		List<TransportCat> liste=new ArrayList<TransportCat>();
		
		for(int i=0;i<listIdentifiantTransCat.size();i++)
		{
			liste.add(metiertrcat.getTransportCat(Integer.parseInt(listIdentifiantTransCat.get(i))));
		}
		
		client.setListTransportTrCat(liste);
		}
		if(cat.compareTo("Pro")==0)
		{
			
		//s'il s'agit d'un client professionnelle
		//transport categories
		
		
		//ville de la société
		client.setVille_Societe(meteirv.getVille(IdCicm));
		
		//gln
		GLN gln=new GLN(meteirv.getVille(IdCicm), client,listeExpgln.get(0), listgln.get(0), client.getRue_Societe(), true);
		
		client.getList_GLN().add(gln);
		//ville postal
		client.setVille_postal(meteirv.getVille(IdCip));
		
		//abonnement
		//récupérer l'abonnement concerné

		if(this.userType.compareTo("Temporary")!=0)
		{
		
		Abonnement abonnement=(Abonnement) metierPrix.getStrategiePrix(IdAbonnement);
		
		ClientAbonnementId clabi=new ClientAbonnementId(client,(Abonnement) metierPrix.getStrategiePrix(IdAbonnement));
		ClientAbonnement cbn=new ClientAbonnement();
		cbn.setPk(clabi);
		//date Début
		SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
		
		Calendar DateDebAbn = Calendar.getInstance();
		
		cbn.setDateDebut(DateDebAbn.getTime());
		
		if(abonnement.getPeriode().compareTo("Month")==0)
		DateDebAbn.add(Calendar.MONTH,abonnement.getNbrPeriode());
		else
		DateDebAbn.add(Calendar.YEAR,abonnement.getNbrPeriode());
		//date fin
		cbn.setDateFin(DateDebAbn.getTime());
		//ajouter l'abonnement
		
	      client.getCABs().add(cbn);
		}
		}
		
		//cid
		client.setCID(cid);
		
		metier.addUtilisateur(client);
		
		
		if(client.getTranExp().compareTo("Exp")==0)
		{
			if(client.getType_client().compareTo("Pri")==0)
			return "ManagePrivateShippers.xhtml";
			else
			return "ManageProShippers.xhtml";	
		}
		
		else
		{
			if(client.getType_client().compareTo("Pri")==0)
			return "ManagePrivateCarriers.xhtml";
			else
			return "ManageProCarriers.xhtml";	
		}
		
		
	}

	//vérifier si lemot de passe saisis est correcte
	
	public boolean verify()
	{
		if(PasswordRe.compareTo(client.getPassword())==0)
			return true;
		else
			return false;
	}

	public List<Pays> getListPays()
	{
		 
		return meteirp.getAllPaysActive();
	}
	
	
	 public void onCountryChange(Long i) {
		 
		 if(i==0)
		 {
			 if(IdC!=0)
		        {
		        	
				 listeEtat= meteirp.getPays(IdC).getListEtat();
		        }
		        else
		        {
		        	listeEtat=null;
		        }
		 }
		 
		 if(i==1)
		 {
			 if(IdCcm!=0)
		        {
		        	
				 listeEtatcm= meteirp.getPays(IdCcm).getListEtat();
		        	
		        }
		        else
		        {
		        	listeEtatcm=null;
		        }
		 }
		 
		 if(i==2)
		 {
			 if(IdCp!=0)
		        {
				 listeEtatp= meteirp.getPays(IdCp).getListEtat();
		        }
		        else
		        {
		        	listeEtatp=null;
		        }
		 }
		 
		 
		 
	        
	    }
	 
	 public void onStateChange(Long i) {
		
		 
		 if(i==0)
		 {
			 if(IdS!=0)
		        {
		        	
		        	listeVille=metiere.getEtat(IdS).getListVille();
		        }
		        else
		        {
		        	listeVille=null;
		        }
		 }
		 
		 if(i==1)
		 {
			 if(IdScm!=0)
		        {
		        	
		        	listeVillecm=metiere.getEtat(IdScm).getListVille();
		        }
		        else
		        {
		        	listeVillecm=null;
		        }
		 }
		 
		 if(i==2)
		 {
			 if(IdSp!=0)
		        {
		        	listeVillep=metiere.getEtat(IdSp).getListVille();
		        }
		        else
		        {
		        	listeVillep=null;
		        }
		 }
		   
	    }
	 
	 
	 public void generer(String Type)
	 {
		
		 cid=cidgeneration(Type);
		 
	 }
	 
	 //Génération de cid
	 public Long cidgeneration(String Type)
	 {
		 Long i;
		 Long a=null;
		 if(metier.lastCID(Type).size()!=0)
		 {
			 a=metier.lastCID(Type).get(metier.lastCID(Type).size()-1).getCID();
		 }
		 System.out.println("hiiiii"+a);
		 if(a==null)
		 {
		     if(Type.compareTo("Tra")==0)
		     {
		    	 i=new Long(10000001);
		    	 
		     }
		     else
		     {
		    	 i=new Long(10000000);
		     }
		 }
		 else
		 {
			 i=a+2;
		 }
		 
		 return i;
	 }
	 
	 //liste des categorie de transport 
	 
	 public void onChangeListTransport()
	 {
		 System.out.println("ll"+listeTransport.size());
		 if(listeTransport.size()!=0)
		 {
			 listTransportCat=new ArrayList<TransportCat>();
			 for(int i=0;i<listeTransport.size();i++)
			 {
			   listTransportCat.addAll(metiertrcat.getAllActiveTransportCat(Integer.parseInt(listeTransport.get(i))));
			 }
		 }
		 else
		 {
			 listTransportCat=null;
		 }
	 }
	 
	 //liste des languages
	 public List<Langue> listeLangauge()
	 {
		 return metierLangue.getAllActiveLangues();
	 }
	 
	
}
